package com.yootk.main;

import com.yootk.config.EmpConfig;
import com.yootk.listener.*;
import com.yootk.vo.Emp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * Bean生命周期管理
 */
public class Demo09_Bean {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo09_Bean.class);

    public static void main(String[] args) throws Exception {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }

    /**
     * Bean的初始化与销毁
     */
    public static void test1() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-base.xml");
        MessageListener listener = context.getBean(MessageListener.class);
        for (int x = 0; x < 3; x++) {   // 接收消息内容
            listener.onReciever("沐言科技：www.yootk.com");
        }
        context.registerShutdownHook();     // 关闭的钩子处理
//        context.close();    // 关闭容器
    }

    /**
     * InitializingBean和DisposableBean
     */
    public static void test2() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.scan("com.yootk.listener");     // 定义扫描包
        context.register(MessageListener2.class);
        context.refresh();  // 刷新操作
        MessageListener2 listener = context.getBean(MessageListener2.class);
        for (int x = 0; x < 3; x++) {
            listener.onReciever("沐言科技：www.yootk.com");
        }
        context.close();
    }

    /**
     * JSR-250注解管理生命周期
     */
    public static void test3() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.scan("com.yootk.listener");     // 定义扫描包
        context.register(MessageListener3.class);
        context.refresh();  // 刷新操作
        MessageListener3 listener = context.getBean(MessageListener3.class);
        for (int x = 0; x < 3; x++) {
            listener.onReciever("沐言科技：www.yootk.com");
        }
        context.close();
    }

    /**
     * Lifecycle生命周期处理规范
     */
    public static void test4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.scan("com.yootk.listener");     // 定义扫描包
        context.register(MessageListener4.class);
        context.refresh();  // 刷新操作
        context.start();    // 启动容器
        MessageListener4 listener = context.getBean(MessageListener4.class);
        for (int x = 0; x < 3; x++) {
            listener.onReciever("沐言科技：www.yootk.com");
        }
        context.close();
    }

    /**
     * SmartLifecycle生命周期扩展
     */
    public static void test5() throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.scan("com.yootk.listener");     // 定义扫描包
        context.register(MessageListener5.class, NewsListener.class);
        context.refresh();  // 刷新操作
//        context.start();    // 已经配置了自动的启动方式，不再手工启动容器
        // 启动消息线程
        new Thread(() -> {
            MessageListener5 listener = context.getBean(MessageListener5.class);
            for (int x = 0; x < 3; x++) {
                listener.onReciever("沐言科技：www.yootk.com");
            }
        }, "MessageThread").start();
        // 启动新闻线程
        new Thread(() -> {
            NewsListener listener = context.getBean(NewsListener.class);
            for (int x = 0; x < 3; x++) {
                listener.onReciever("沐言科技：www.yootk.com");
            }
        }, "NewsThread").start();
        TimeUnit.SECONDS.sleep(3);  // 延迟3秒结束
        context.close();
    }

    /**
     * SmartInitializingSingleton回调处理
     */
    public static void test6() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EmpConfig.class);  // Bean注册
        context.refresh();
        Emp emp = context.getBean(Emp.class);
        LOGGER.info("【雇员信息】姓名：{}、工资：{}", emp.getEname(), emp.getSalary());
    }
}
